package socketVersion.Backend.Cars;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Set;

public class CarServerImplementation implements CarsInterface{

	private Hashtable<String,Car> cars;
	private Hashtable<String,LinkedList<Integer>> reservations;
	
	public CarServerImplementation(){
		cars = new Hashtable<String, Car>();
		reservations = new Hashtable<String,LinkedList<Integer>>();
	}
	
	@Override
	public boolean addCars(int id, String location, int numCars, int price) {
		System.out.println("Adding car: ("+id+","+location+","+numCars+","+price+")");
		if(numCars <0 || price <=0){
			return false;
		}
		
		Car c = cars.get(location);
		if(c!=null){
			cars.remove(location);
			cars.put(location, new Car(location,price,c.getNumCars()+numCars));
		}
		else{
			cars.put(location, new Car(location,price,numCars));
		}
		return true;
	}

	@Override
	public boolean deleteCars(int id, String location) {
		System.out.println("Deleting car: ("+id+","+location+")");
		LinkedList<Integer> customers = new LinkedList<Integer>();
		customers = reservations.get(location);
		if(customers == null){
			cars.remove(location);
			return true;
		}
		
		if(customers.size()!=0){
			return false;
		}
		else{
			cars.remove(location);
			return true;
		}
	}

	@Override
	public int queryCars(int id, String location) {
		System.out.println("Querying car: ("+id+","+location+")");
		Car c = cars.get(location);
		if(c==null){
			return 0;
		}
		int totalCars = c.getNumCars();
		LinkedList<Integer> customers = reservations.get(location);
		if(customers==null){
			return totalCars;
		}
		else{
			return totalCars - customers.size();
		}
	}

	@Override
	public int queryCarsPrice(int id, String location) {
		System.out.println("Querying car price: ("+id+","+location+")");
		Car c = cars.get(location);
		if(c==null){
			return 0;
		}
		else{
			return c.getPrice();
		}
	}

	@Override
	public boolean reserveCar(int id, int customer, String location) {
		System.out.println("Reserving car: ("+id+","+customer+","+location+")");
		Car c = cars.get(location);
		if(c==null){
			return false;
		}
		else{
			if(queryCars(id,location)>0){
				LinkedList<Integer> customers = reservations.get(location);
				if(customers==null){
					customers = new LinkedList<Integer>();
					customers.add(customer);
					reservations.put(location, customers);
				}
				else{
					customers.add(customer);
				}
				return true;
			}
			else{
				return false;
			}
		}
	}

	
	
	@Override
	public boolean deleteReservation(int id, int customer) {
		System.out.println("Deleting reservation: ("+id+","+customer+")");
		Set<String> keys = reservations.keySet();
		Object[] keyArray = keys.toArray();
		for(int i = 0;i<keyArray.length;i++){
			LinkedList<Integer> customers = reservations.get((String)keyArray[i]);
			while(customers.contains((Integer)customer)){
				customers.remove((Integer)customer);
			}
			
		}
		
		return true;
	}

	@Override
	public int calculateBill(int id, int customer) {
		System.out.println("Calculating bill: ("+id+","+customer+")");
		Set<String> keys = reservations.keySet();
		Object[] keyArray = keys.toArray();
		int totalBill = 0;
		
		for(int i = 0;i<keyArray.length;i++){
			LinkedList<Integer> customers = reservations.get((String)keyArray[i]);
			int noRooms = 0;
			for(int j = 0;j<customers.size();j++){
				if(customer == (int)customers.get(j)){
					noRooms++;
				}
			}
			totalBill+= (noRooms*queryCarsPrice(-1,(String)keyArray[i]));
		}
		
		return totalBill;
	}
	

}
